// pages/store/store.js
import { Store } from '../../model/store.js';
var store = new Store();
import { User } from '../../model/user.js';
var user = new User();
var appInst =  getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hidden: false,
    price: '',
    invite_money: '',
    price_one: '',
    new_user_count: 0,
    product_count: 0,
    user_money: 0,
    user_money_one: 0,
    active: 0,
    fission: 0,
    icon: {
      home: '../../utils/image/home.png',
      home_active: '../../utils/image/home-active.png',
      order: '../../utils/image/order.png',
      order_active: '../../utils/image/order-active.png',
      user: '../../utils/image/user.png',
      user_active: '../../utils/image/user-active.png'
    },
    userInfo: {},
    store_user: {}
  },
  /**
   * 切换菜单
   */
  onChangeTabbar(e) {
    if (e.detail == 1) {
      wx.switchTab({
        url: '../order/order',
      })
    } else if (e.detail == 2) {
      wx.switchTab({
        url: '../user/user',
      })
    }
  },
  openInviteRank() {
    wx.navigateTo({
      url: 'invite_rank',
    })
  },
  openOwnerRank() {
    wx.navigateTo({
      url: 'store_rank',
    })
  },
  openStoreOwnerSaleRank(){
    wx.navigateTo({
      url: 'store_owner_list',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中',
    });
    this.getUserInfo();
  },
  /**
   * 修改title
   */
  onShow:function(){
    if(appInst.globalData.appstorename!='美业团')
      appInst.settitle();
  },
  /**
   * 获取用户信息
   */
  getUserInfo() {
    user.getUserInfo((result) => {
      this.setData({
        userInfo: result,
      })
      this.getStaffInfo();
    })
  },
  getStaffInfo() {
    store.getStoreUserStaff((res) => {
      this.setData({
        hidden: true,
        price: res.price,
        invite_money: res.invite_money,
        price_one: res.price_one,
        new_user_count: res.new_user_count,
        product_count: res.product_count,
        order_count: res.order_count,
        user_money: res.user_money,
        user_money_one: res.user_money_one,
        store_user: res.store_user,
        fission: res.fission
      })
      wx.hideLoading();
    })
  },
  openProduct() {
    wx.switchTab({
      url: '/pages/home/home',
    })
  },
  openProductCount() {
    wx.navigateTo({
      url: 'store_owner_product',
    })
  },
  openStore() {
    wx.navigateTo({
      url: 'search',
    })
  },
  openRank() {
    wx.navigateTo({
      url: 'rank',
    })
  },
  openLie() {
    wx.navigateTo({
      url: 'user_rank',
    })
  },
  openCode() {
    wx.navigateTo({
      url: '../user/code',
    })
  },
  openUser() {
    wx.navigateTo({
      url: 'store_owner_user',
    })
  },
  openCheng() {
    wx.navigateTo({
      url: 'cheng_rank',
    })
  },
  openOrder() {
    wx.navigateTo({
      url: 'order',
    })
  },
  openUserMoneyList() {
    if(this.data.store_user.store) {
      wx.navigateTo({
        url: `../admin/user_money_list?name=${this.data.store_user.store.name}&id=${this.data.store_user.store_id}`,
      })
    } else {
      wx.navigateTo({
        url: '../admin/user_money',
      })
    }
  },
  openUserMoney() {
    wx.navigateTo({
      url: 'store_owner_money',
    })
  },
  openFission() {
    if(this.data.store_user.store) {
      wx.navigateTo({
        url: `../admin/fission_list?name=${this.data.store_user.store.name}&store_id=${this.data.store_user.store_id}`,
      })
    }else{
      wx.navigateTo({
        url: '../admin/fission',
      })
    }
  }
})